$(function(){
	/*var subPrdCode = $("#subPrdCode").val();
	feeYieldList(subPrdCode);*/
});
// 费率列表
function feeYieldList(subPrdCode){
	$("#feeYieldList").jqGrid({
		caption: '费用列表',
		url: '/solution/business/customerEquity/prdFeeYield/queryFeeYieldBySubPrdCodeListPage.do',
		datatype: 'local',
		colNames: ['ID', '费用名称', '费用类型', '', '费率类型', '费率（%）', '费用总金额（元）'],    
		colModel: [
				  {name:'feeId',index:'feeId',key: true,hidden: true },
				  {name:'feeName',index:'feeName',sortable: false,resizable:true },
				  {name:'oldFeeName',index:'oldFeeName', sortable: false,resizable:true },
				  {name:'feeRateType',index:'feeRateType',hidden: true },
				  {name:'feeRateTypeNm',index:'feeRateTypeNm',sortable: false,resizable:true },
			      {name:'feeRate',index:'feeRate', sortable: false, resizable:true, formatter:isShowFeeTate },
				  {name:'feeAggregateAmouunt',index:'feeAggregateAmouunt',  sortable:false,resizable:true}
				  ],
		rowNum:20,        
		rowList:[20,30,50],  
		rownumbers : true,
		postData:{
			'sp[subPrdCode]': subPrdCode
		},
		rownumWidth : 50,
		prmNames : {
			search : "search",
			page : "pageNo",
			rows : "limit"
		},
		height : 'auto',
		width : 'auto',
		autowidth : true,
		shrinkToFit : true,
		viewrecords : true,
		cellEdit : false,
		shrinkToFit : true,
		grouping : false,
		subGrid: true,
		subGridRowExpanded: subGridRowExpanded,
		subGridRowColapsed: function(subgrid_id, row_id) {
			$("#subGridTBId").val("");
		},
		jsonReader : {
			root : "items", // 结果集
			records : "total", // 总记录数
			total : "pageCount", // 总页数
			page : "pageNo", // 当前页
			repeatitems : false
		},
		multiselect : true,
		viewrecords : true,
		hiddengrid: false, //
		hidegrid: false, // 隐藏/显示按钮是否显示
		pager: "#feeYieldListPage",
		gridComplete: function() {
			var ids = $("#feeYieldList").jqGrid('getDataIDs');
			for (var i = 0; i < ids.length; i++) {
				var id = ids[i];
				var rowData = $("#feeYieldList").jqGrid('getRowData', id);
				var feeRateType = rowData.feeRateType;
				if (feeRateType != '1') {
					$("#" + id).find("span").parent().parent().unbind("click");
					$("#" + id).find("span").remove();
				}
			}
		}
	});
	var feeYieldGrid  =  $("#feeYieldList").navGrid('#feeYieldListPage',{edit:false,add:false,del:false,search:false});
	
	var params = {'add' : 'addFeeYieldTemp','del' : 'delFeeYieldTemp'};
	var permissionButton = Permission_Button.configPermissionButton(params);
	var btnStr = Permission_Button.linkAllBtn(permissionButton);
	var butArr = [];
	if(permissionButton.add){
		var caption = $(permissionButton.add).text();
		var icon = $(permissionButton.add).find("i").attr("class");
		var addPrams = {  
				id: "feeYieldAdd",
				caption:caption,   
				buttonicon:icon,  
				position:"last", 
				onClickButton: function(){
					addFeeYield(subPrdCode);
				}
			}
		butArr.push(addPrams)
	}
	if(permissionButton.del){
		var caption = $(permissionButton.del).text();
		var icon = $(permissionButton.del).find("i").attr("class");
		var delPrams = {  
				id: "feeYieldDel",
				caption:caption,   
				buttonicon:icon,  
				position:"last", 
				onClickButton: function(){
					batchDelFeeYield(subPrdCode);
				}
			}
		butArr.push(delPrams)
	}
	if(butArr.length>0){
		for(var i=0;i<butArr.length;i++){
			feeYieldGrid.navButtonAdd("#feeYieldListPage",butArr[i]);
		}
	}
}

function addFeeYield(subPrdCode) {
	window.open("/solution/business/treFeeDetail/treFeeDetailView.do?actionType=add&type=cpyy");
}

function batchDelFeeYield(subPrdCode) {
	var ids =  $("#feeYieldList").jqGrid('getGridParam', 'selarrrow');//选择多选
	if(ids.length == 0){
		ctools.alert('请选择需要删除的费用!',"","warning");
		return;
	}
	ctools.confirm({text:"是否确认删除费用？"},function(isConfirm){
		if(isConfirm){
			if (ids == null || ids == "") {
				return;
			}
			$.ajax({
				type: 'POST',
				url: "/solution/business/customerEquity/prdFeeYield/deletePrdFeeDetail.do",
				dataType:'json',
				data:{
					"ids" : ids.join(",")
				},
				success: function(data){
					if (data && data.success) {
						ctools.alert_sweet("删除成功", "success", "" , function(){
							queryByCondtion(true, subPrdCode);
						});
					}
				},
				error:function(xhr){
					switch(xhr.status){
						case 403:sweetAlert("对不起，您无此权限！","","error");break;
						case 404:sweetAlert("对不起，无此页面！", "","error");break;
						case 500:sweetAlert("内部错误，请联系管理员！","","error");break;  
						case 504:sweetAlert("超时，请联系管理员！", "","error");break;  
						case 417:sweetAlert("内部错误，请联系管理员！", "","error");break;  
					}
				}
			});
		}
	});
}


//自动适应宽度
$(window).resize(function(){
	var parentWidth = $("#feeYieldListDiv").width();
	$("#feeYieldList").jqGrid('setGridWidth', parentWidth );
});

// 浮动费率列表
function subGridRowExpanded(subgrid_id, row_id) {
	var rowData = $("#feeYieldList").jqGrid('getRowData', row_id);
	var subgrid_table_id = subgrid_id+"_t";
	$("#subGridTBId").val(subgrid_table_id);
	var pager_id = "p_"+subgrid_table_id;
	$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
 		
	jQuery("#"+subgrid_table_id).jqGrid({
		url: '/solution/business/customerEquity/prdFeeYield/queryChangFeeYieldByFeeIdListPage.do',
		datatype: "json",
		colNames: ['起期', '止期', '费率（%）'],    
		colModel: [
			  {name:'rateBeginDate',index:'rateBeginDate',sortable: false,resizable:true },
			  {name:'rateEndDate',index:'rateEndDate', sortable: false,resizable:true },
		      {name:'changeFeeRate',index:'changeFeeRate', sortable: false, resizable:true }
			  ],
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        postData:{
        	'sp[feeId]': rowData.feeId
		},
        rownumWidth: 50,
        prmNames: {
        	        search: "search", 
        	        page: "pageNo",
        	        rows: "limit" 
        	       },
        height: 'auto',
        width: false,
        autowidth:true,
        shrinkToFit:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },
        multiselect: false,
        pager: "#" + pager_id, 	
        viewrecords: true,
        hidegrid: false,	
		gridComplete: function(){
			
		}
	});
	jQuery("#"+subgrid_table_id).navGrid('#'+pager_id,{edit:false,add:false,del:false,search:false});
}

function isShowFeeTate(text, options, rData) {
	if (rData['feeRateType'] == '1') return "-";
	if (text == null) return "";
	return text;
}

function queryByCondtion(flag, subPrdCode) {
	var postData = $("#feeYieldList").jqGrid("getGridParam", "postData");
	if (!subPrdCode) {
		subPrdCode = $("#subPrdCode").val();
	}
    $.extend(postData,{
    	'sp[subPrdCode]': subPrdCode
    });
    if (flag) {
    	$("#feeYieldList").jqGrid('setGridParam',{"datatype": "json"}).trigger("reloadGrid",[{page:1}]);//重新载入Grid表格
    } else {
    	$("#feeYieldList").jqGrid('setGridParam',{"datatype": "json"}).trigger("reloadGrid");
    }
}